﻿Public Class DayCell
    Inherits UserControl

    Public Event DayClicked As Action

    ' 当前日期
    Public Property DateValue As Date
    ' 显示的主要文本（通常为公历日期）
    Public Property MainText As String = ""
    ' 显示的次要文本（通常为农历或节气信息）
    Public Property SubText As String = ""
    ' 当前月份标识
    Public Property IsCurrentMonth As Boolean = True
    ' 是否有任务
    Public Property HasTasks As Boolean = False

    ' 新增：显示额外的自定义文本
    Public Property OtherText As String

    Public Sub New()
        ' 启用双缓冲，减少闪烁
        SetStyle(ControlStyles.DoubleBuffer Or ControlStyles.UserPaint Or ControlStyles.AllPaintingInWmPaint, True)
        UpdateStyles()

        Me.Margin = New Padding(1)
        Me.BackColor = Color.White
        Me.Cursor = Cursors.Hand
        AddHandler Me.Click, Sub() RaiseEvent DayClicked()
        AddHandler Me.Paint, AddressOf OnPaintCell
    End Sub

    Private Sub OnPaintCell(sender As Object, e As PaintEventArgs)
        Dim g = e.Graphics
        g.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias

        ' 背景
        Using b As New SolidBrush(If(IsCurrentMonth, Color.White, Color.FromArgb(245, 245, 245)))
            g.FillRectangle(b, Me.ClientRectangle)
        End Using

        ' 今日高亮边框
        If DateValue.Date = Date.Today Then
            Using p As New Pen(Color.FromArgb(66, 133, 244), 2)
                g.DrawRectangle(p, New Rectangle(1, 1, Me.Width - 3, Me.Height - 3))
            End Using
        Else
            Using p As New Pen(Color.Gainsboro)
                g.DrawRectangle(p, New Rectangle(0, 0, Me.Width - 1, Me.Height - 1))
            End Using
        End If

        ' 公历日（左上）
        Using f As New Font("Microsoft YaHei", 9, FontStyle.Bold)
            Dim clr = If(IsCurrentMonth, Color.Black, Color.Silver)
            TextRenderer.DrawText(g, MainText, f, New Point(8, 6), clr)
        End Using

        ' 农历/节气（左下）
        Using f2 As New Font("Microsoft YaHei", 6.5F, FontStyle.Regular)
            Dim y = Me.Height - 20
            Dim clr = If(IsCurrentMonth, Color.DimGray, Color.Silver)
            TextRenderer.DrawText(g, SubText, f2, New Point(8, y), clr)
        End Using

        ' 右上角：任务小圆点
        If HasTasks Then
            Dim r As New Rectangle(Me.Width - 14, 6, 8, 8)
            Using b As New SolidBrush(Color.FromArgb(46, 204, 113)) ' 绿色
                g.FillEllipse(b, r)
            End Using
        End If
    End Sub

    Protected Overrides Sub OnMouseEnter(e As EventArgs)
        MyBase.OnMouseEnter(e)
        Me.BackColor = Color.FromArgb(250, 250, 250)
        Me.Invalidate()
    End Sub

    Protected Overrides Sub OnMouseLeave(e As EventArgs)
        MyBase.OnMouseLeave(e)
        Me.BackColor = If(IsCurrentMonth, Color.White, Color.FromArgb(245, 245, 245))
        Me.Invalidate()
    End Sub
End Class
